package com.example.book.config;

import com.example.book.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/19
 * Time: 22:07
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 也可以不用spring自动管理，去掉 LoginInterceptor 类中的 @Component 注解和 LoginInterceptor 的对象注入
//        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**");
//        registry.addInterceptor(loginInterceptor)
//                .addPathPatterns("/**") // 对哪些路径生效
//                .excludePathPatterns("/user/login"); // 排除掉哪些路径
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/book/**"); // 对哪些路径生效
    }
}
